<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
      <el-form-item label="区域搜索:">
        <el-input v-model="queryParams.regionName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 新建按钮 -->
    <el-button type="primary" style="margin-top: 10px; margin-bottom: 10px;" @click="handleAdd">
      + 新建
    </el-button>

    <!-- 区域列表 -->
    <el-table v-loading="loading" :data="regionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" prop="id" />
      <el-table-column label="区域名称" align="center" prop="regionName" />
      <el-table-column label="点位数" align="center" prop="nodeCount" />
      <el-table-column label="备注说明" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="getRegionInfo(scope.row)"
            v-hasPermi="['manage:node:list']">查看详情</el-button>
          <el-button link type="primary" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:region:edit']">修改</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:region:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="regionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="区域名称" prop="regionName">
          <el-input v-model="form.regionName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog title="区域详情" v-model="regionInfoOpen" width="600px" append-to-body>
      <el-form :model="form" label-width="80px">
        <el-form-item label="区域名称">
          <el-input v-model="form.regionName" disabled />
        </el-form-item>
      </el-form>
      <label style="font-weight: bold; margin-bottom: 5px; display:block;">包含点位：</label>
      <el-table :data="nodeList" border>
        <el-table-column label="序号" type="index" width="70" align="center" />
        <el-table-column label="点位名称" align="center" prop="nodeName" />
        <el-table-column label="设备数量" align="center" prop="vmCount" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup name="Region">
import { listRegion, getRegion, delRegion, addRegion, updateRegion } from "@/api/manage/region";
import { listNode } from "@/api/manage/node";

const { proxy } = getCurrentInstance();

const regionList = ref([]);
const open = ref(false);
const loading = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    regionName: null,
  },
  rules: {
    regionName: [
      { required: true, message: "区域名称不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

// 详情相关
const nodeList = ref([]);
const regionInfoOpen = ref(false);

/** 查询列表 */
function getList() {
  loading.value = true;
  listRegion(queryParams.value).then(response => {
    regionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置表单 */
function reset() {
  form.value = {
    id: null,
    regionName: null,
    remark: null,
    pointCount: null
  };
  proxy.resetForm("regionRef");
}

/** 新增 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加区域管理";
}

/** 修改 */
function handleUpdate(row) {
  reset();
  const _id = row.id;
  getRegion(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改区域管理";
  });
}

/** 删除 */
function handleDelete(row) {
  const _ids = row.id;
  proxy.$modal.confirm('是否确认删除区域编号为"' + _ids + '"的数据项？').then(() => {
    return delRegion(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 提交表单 */
function submitForm() {
  proxy.$refs["regionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRegion(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRegion(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 取消 */
function cancel() {
  open.value = false;
  reset();
}

/** 查看详情 */
function getRegionInfo(row) {
  reset();
  const _id = row.id;

  // 查询区域信息
  getRegion(_id).then(response => {
    form.value = response.data;
  });

  // 查询点位列表
  listNode({ regionId: _id }).then(response => {
    nodeList.value = response.rows;
  });

  regionInfoOpen.value = true;
}

// 初始化加载数据
getList();
</script>
